//
//  TKYMessageCenterController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/25.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYMessageCenterController.h"
#import "TKYMessageTitleView.h"
#import "TKYTaskInformationViewCell.h"
#import "TKYNoticeViewCell.h"
#import "TKYHomePageDetitleController.h"
//任务消息cell
#define KTaskInformationViewCellID @"TKYTaskInformationViewCell"
//通知提醒
#define KNoticeViewCellID @"TKYNoticeViewCell"

@interface TKYMessageCenterController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) TKYMessageTitleView *messageTitleView;
//任务消息
@property (nonatomic, strong) BaseTabelView *taskInformationTableV;
//通知提醒
@property (nonatomic, strong) BaseTabelView *noticeTableV;


@end

@implementation TKYMessageCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置界面
    [self setUPUI];
}

/**
 设置界面
 */
- (void)setUPUI{
    self.navigationItem.title = @"消息中心";
    self.navigationController.navigationBar.barTintColor = KNavColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
    
    [self.view addSubview:self.messageTitleView];
    [self.view addSubview:self.taskInformationTableV];
    [self.view addSubview:self.noticeTableV];
    self.taskInformationTableV.hidden = NO;
    self.noticeTableV.hidden = YES;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.taskInformationTableV) {
        return 120;
    }else{
        return 80;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.taskInformationTableV) {
        return 2;
    }else{
        return self.messageListArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.taskInformationTableV) {
        TKYTaskInformationViewCell *taskCell = [tableView dequeueReusableCellWithIdentifier:KTaskInformationViewCellID];
        //cell.textLabel.text = @"taskInformationTableV";
        return taskCell;
    }else{
        TKYNoticeViewCell *noticeCell = [tableView dequeueReusableCellWithIdentifier:KNoticeViewCellID];
        noticeCell.messageListModel = self.messageListArr[indexPath.row];
        return noticeCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.taskInformationTableV) {
        TKYHomePageDetitleController *homePageDetitleVC = [TKYHomePageDetitleController new];
        [self.navigationController pushViewController:homePageDetitleVC animated:YES];
    }else{//消息通知
        TKYMessageListModel *messageListModel = self.messageListArr[indexPath.row];
        messageListModel.status = 1;
        //修改已读状态
        [self requestUpdateMessageStatusWithMessageID:messageListModel.ID];
        //重新请求数据
        //[self requestMessageInfoList];
        
        [self.noticeTableV reloadData];
    }
}

/**
 根据id修改通知提醒为已读
 */
- (void)requestUpdateMessageStatusWithMessageID:(NSString *)ids{
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiBaseUrl,KupdateMessageStatusUR];
    NSDictionary *parameterDic = @{@"token":[[NSUserDefaults standardUserDefaults]objectForKey:kToken],
                                   @"id":ids
                                       };
    [KHttpRequestTool POST:url parameters:parameterDic success:^(id response) {
        if ([response[@"code"]integerValue] == 0) {
           // [SVProgressHUD showInfoWithStatus:];
        }
        [self.noticeTableV reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (TKYMessageTitleView *)messageTitleView{
    if (_messageTitleView == nil) {
        _messageTitleView = [[TKYMessageTitleView alloc]initWithFrame:CGRectMake(0, MyNavBarHeight, kScreenWidth, 45)];
        //_messageTitleView.backgroundColor = [UIColor redColor];
        int i;
        i = 0;
#warning 暂时还没有数据
//        if (i == 1 ) {
//            [_messageTitleView.taskInformationButton pp_showBadge];
//        }else{
//            [_messageTitleView.taskInformationButton pp_hiddenBadge];
//
//        }
//        
        if (self.isMessageBadge == NO ) {
            [_messageTitleView.noticeButton pp_hiddenBadge];
        }else{
            [_messageTitleView.noticeButton pp_showBadge];
            
        }
        __weak typeof(self) weakSelf = self;
        //任务消息
        _messageTitleView.taskInformationBlock = ^(UIButton *button) {
            NSLog(@"任务消息");
            weakSelf.taskInformationTableV.hidden = NO;
            weakSelf.noticeTableV.hidden = YES;
            
        };
        
        //通知提醒
        _messageTitleView.noticeBlock = ^(UIButton *button) {
            weakSelf.taskInformationTableV.hidden = YES;
            weakSelf.noticeTableV.hidden = NO;
           NSLog(@"通知提醒");
        };
        
    }
    return _messageTitleView;
}

- (BaseTabelView *)noticeTableV{
    if (_noticeTableV == nil) {
        _noticeTableV = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, self.messageTitleView.kx_bottom, kScreenWidth, kScreenHeight- MyNavBarHeight -self.messageTitleView.kx_height) style:UITableViewStylePlain];
        _noticeTableV.delegate = self;
        _noticeTableV.dataSource = self;
        _noticeTableV.backgroundColor = kMainColour;
        [_noticeTableV registerNib:[UINib nibWithNibName:@"TKYNoticeViewCell" bundle:nil] forCellReuseIdentifier:KNoticeViewCellID];
        
    }
    return _noticeTableV;
}

- (BaseTabelView *)taskInformationTableV{
    if (_taskInformationTableV == nil) {
        _taskInformationTableV = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, self.messageTitleView.kx_bottom, kScreenWidth, kScreenHeight- MyNavBarHeight -self.messageTitleView.kx_height) style:UITableViewStylePlain];
        _taskInformationTableV.delegate = self;
        _taskInformationTableV.dataSource = self;
        [_taskInformationTableV registerNib:[UINib nibWithNibName:@"TKYTaskInformationViewCell" bundle:nil] forCellReuseIdentifier:KTaskInformationViewCellID];
        _taskInformationTableV.backgroundColor = kMainColour;
    }
    return _taskInformationTableV;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
